package com.hifar.test.equip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.log.AutoLogUtils;
import com.hifar.plat.baseModal.service.IBaseOssFilesService;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.PageResult;
import com.hifar.sys.SystemUtil;
import com.hifar.test.equip.pojo.HfEnvClamp;
import com.hifar.test.equip.pojo.VO.HfEnvClampQueryParamsVO;
import com.hifar.test.equip.service.IHfEnvClampService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源-设备 前端控制器 default
 * </p>
 *
 * @author wangkepeng
 * @since 2024-12-11 11:58:26
 */
@RestController
@RequestMapping("/api/busi/test/envClamp")
@Api(tags = "资源-夹具功能")
public class HfEnvClampController {

    @Autowired
    private IHfEnvClampService envClampService;

    @Autowired
    private IBaseOssFilesService baseOssFilesService;

    @Autowired
    private AutoLogUtils autoLogUtils;

    @PostMapping(value = "/listPage")
    @ApiOperation(value = "资源-夹具功能:分页查询列表")
    public BusiResponse listPage(@RequestBody @Validated HfEnvClampQueryParamsVO queryParamsVO) {
        QueryParamBean<HfEnvClamp> queryParamBean = new QueryParamBean<>(queryParamsVO, HfEnvClamp.class);
        QueryWrapper<HfEnvClamp> queryWrapper = queryParamBean.getQueryWrapper();
        Page<HfEnvClamp> page = queryParamBean.getPage();
        Page<HfEnvClamp> pageData = envClampService.page(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);

        return BusiResponse.OK(pageResult);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "资源-夹具功能:新增")
    public BusiResponse add(@RequestBody @Validated HfEnvClamp envClamp) {
        envClampService.save(envClamp);
        autoLogUtils.addLog("环境试验-夹具台账", String.format("新增夹具:【%s】", envClamp.getClampCode()));
        return BusiResponse.ok("添加成功");
    }


    @PostMapping(value = "/edit")
    @ApiOperation(value = "资源-夹具功能:编辑")
    public BusiResponse edit(@RequestBody @Validated HfEnvClamp envClamp) {
        envClampService.updateById(envClamp);
        autoLogUtils.modifyLog("环境试验-夹具台账", String.format("编辑夹具:【%s】", envClamp.getClampCode()));
        return BusiResponse.ok("编辑成功！");
    }


    @GetMapping("/queryById")
    @ApiOperation(value = "资源-夹具功能:根据id查询数据")
    public BusiResponse queryById(@RequestParam(name = "id") String id) {
        HfEnvClamp hfEnvClamp = envClampService.getById(id);
        return BusiResponse.OK(hfEnvClamp);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "资源-夹具功能:逻辑删除")
    public BusiResponse delete(@RequestBody List<String> idList) {

        List<HfEnvClamp> clampList = envClampService.listByIds(idList);
        envClampService.removeByIds(idList);
        String delClampCodes = clampList.stream()
                .map(HfEnvClamp::getClampCode)
                .distinct()
                .collect(Collectors.joining(","));
        autoLogUtils.deleteLog("环境试验-夹具台账", String.format("删除了夹具:【%s】", delClampCodes));
        return BusiResponse.ok("删除成功！");
    }
}